home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-04 / gs24src.zip / GSFONT.C < prev    next >
C/C++ Source or Header  |  1991-11-25  |  9KB  |  290 lines

  1. /* Copyright (C) 1989, 1990, 1991 Aladdin Enterprises.  All rights reserved.
  2.    Distributed by Free Software Foundation, Inc.
  3.  
  4. This file is part of Ghostscript.
  5.  
  6. Ghostscript is distributed in the hope that it will be useful, but
  7. WITHOUT ANY WARRANTY.  No author or distributor accepts responsibility
  8. to anyone for the consequences of using it or for whether it serves any
  9. particular purpose or works at all, unless he says so in writing.  Refer
  10. to the Ghostscript General Public License for full details.
  11.  
  12. Everyone is granted permission to copy, modify and redistribute
  13. Ghostscript, but only under the conditions described in the Ghostscript
  14. General Public License.  A copy of this license is supposed to have been
  15. given to you along with Ghostscript so you can know your rights and
  16. responsibilities.  It should be in a file named COPYING.  Among other
  17. things, the copyright notice and this notice must be preserved on all
  18. copies.  */
  19.  
  20. /* gsfont.c */
  21. /* Font operators for GhostScript library */
  22. #include "gx.h"
  23. #include "memory_.h"
  24. #include "gserrors.h"
  25. #include "gxfixed.h"
  26. #include "gxmatrix.h"
  27. #include "gzstate.h"            /* must precede gxdevice */
  28. #include "gxdevice.h"            /* must precede gxfont */
  29. #include "gschar.h"
  30. #include "gxfont.h"
  31. #include "gxfdir.h"
  32.  
  33. /* Imported procedures */
  34. void    gs_purge_font_from_char_caches(P2(gs_font_dir *, gs_font *));
  35.  
  36. /* Size of cache structures */
  37. extern const uint cached_char_sizeof;
  38. extern const uint cached_fm_pair_sizeof;
  39.  
  40. /* Define the sizes of the various aspects of the font/character cache. */
  41. /*** Big memory machines ***/
  42. #define smax_LARGE 50        /* smax - # of scaled fonts */
  43. #define bmax_LARGE 500000    /* bmax - space for cached chars */
  44. #define mmax_LARGE 200        /* mmax - # of cached font/matrix pairs */
  45. #define cmax_LARGE 5000        /* cmax - # of cached chars */
  46. #define blimit_LARGE 1000    /* blimit/upper - max size of a single cached char */
  47. /*** Small memory machines ***/
  48. #define smax_SMALL 20        /* smax - # of scaled fonts */
  49. #define bmax_SMALL 25000    /* bmax - space for cached chars */
  50. #define mmax_SMALL 40        /* mmax - # of cached font/matrix pairs */
  51. #define cmax_SMALL 500        /* cmax - # of cached chars */
  52. #define blimit_SMALL 100    /* blimit/upper - max size of a single cached char */
  53.  
  54. /* Allocate a font directory */
  55. gs_font_dir *
  56. gs_font_dir_alloc(proc_alloc_t palloc, proc_free_t pfree)
  57. {    /* Try allocating a very large cache. */
  58.     /* If this fails, allocate a small one. */
  59.     gs_font_dir *pdir;
  60. #if !arch_ints_are_short
  61.     pdir = gs_font_dir_alloc_limits(palloc, pfree,
  62.                     smax_LARGE, bmax_LARGE, mmax_LARGE,
  63.                     cmax_LARGE, blimit_LARGE);
  64.     if ( pdir != 0 ) return pdir;
  65. #endif
  66.     return gs_font_dir_alloc_limits(palloc, pfree,
  67.                     smax_SMALL, bmax_SMALL, mmax_SMALL,
  68.                     cmax_SMALL, blimit_SMALL);
  69. }
  70. gs_font_dir *
  71. gs_font_dir_alloc_limits(proc_alloc_t palloc, proc_free_t pfree,
  72.   uint smax, uint bmax, uint mmax, uint cmax, uint upper)
  73. {    register gs_font_dir *pdir = (gs_font_dir *)(*palloc)(1, sizeof(gs_font_dir), "font_dir_alloc(dir)");
  74.     uint cdcount = bmax / cached_char_sizeof + 1;
  75.     uint cdsize = cdcount * cached_char_sizeof;
  76.     uint chsize = (cmax / 5) | 31;        /* a guess */
  77.     struct cached_fm_pair_s *mdata;
  78.     byte *cdata;
  79.     struct cached_char_s **chars;
  80.     if ( pdir == 0 ) return 0;
  81.     /* Round up chsize to a power of 2. */
  82.     while ( chsize & (chsize + 1) ) chsize |= chsize >> 1;
  83.     chsize++;
  84.     mdata = (struct cached_fm_pair_s *)(*palloc)(mmax, cached_fm_pair_sizeof, "font_dir_alloc(mdata)");
  85.     cdata = (byte *)(*palloc)(cdcount, cached_char_sizeof, "font_dir_alloc(cdata)");
  86.     chars = (struct cached_char_s **)(*palloc)(chsize, sizeof(struct cached_char_s *), "font_dir_alloc(chars)");
  87.     if ( mdata == 0 || cdata == 0 || chars == 0 )
  88.        {    if ( chars != 0 ) (*pfree)((char *)chars, chsize, sizeof(struct cached_char_s *), "font_dir_alloc(chars)");
  89.         if ( cdata != 0 ) (*pfree)((char *)cdata, cdcount, cached_char_sizeof, "font_dir_alloc(cdata)");
  90.         if ( mdata != 0 ) (*pfree)((char *)mdata, mmax, cached_fm_pair_sizeof, "font_dir_alloc(mdata)");
  91.         (*pfree)((char *)pdir, 1, sizeof(gs_font_dir), "font_dir_alloc(dir)");
  92.         return 0;
  93.        }
  94.     memset((char *)pdir, 0, sizeof(gs_font_dir));    /* easiest to clear everything first */
  95.     pdir->alloc = palloc;
  96.     pdir->free = pfree;
  97.     pdir->smax = smax;
  98.     pdir->bmax = bmax;
  99.     pdir->mmax = mmax;
  100.     pdir->cmax = cmax;
  101.     pdir->lower = upper / 10;
  102.     pdir->upper = upper;
  103.     pdir->mdata = mdata;
  104.     pdir->cdata = cdata;
  105.     pdir->cdata_size = cdsize;
  106.     pdir->chars = chars;
  107.     pdir->chars_mask = chsize - 1;
  108.     gx_char_cache_init(pdir);
  109.     return pdir;
  110. }
  111.  
  112. /* Macro for linking an element at the head of a chain */
  113. #define link_first(first, elt)\
  114.   if ( (elt->next = first) != NULL ) first->prev = elt;\
  115.   elt->prev = 0;\
  116.   first = elt
  117.  
  118. /* scalefont */
  119. int
  120. gs_scalefont(gs_font_dir *pdir, gs_font *pfont, floatp scale,
  121.   gs_font **ppfont, gs_font **pdfont)
  122. {    gs_matrix mat;
  123.     gs_make_scaling(scale, scale, &mat);
  124.     return gs_makefont(pdir, pfont, &mat, ppfont, pdfont);
  125. }
  126.  
  127. /* makefont */
  128. int
  129. gs_makefont(gs_font_dir *pdir, gs_font *pfont, gs_matrix *pmat,
  130.   gs_font **ppfont, gs_font **pdfont)
  131. {    int code;
  132.     gs_font *prev = 0, *pf_out = pdir->scaled_fonts;
  133.     gs_matrix newmat;
  134.     *pdfont = 0;
  135.     gs_make_identity(&newmat);    /* fill in tags */
  136.     if ( (code = gs_matrix_multiply(&pfont->FontMatrix, pmat, &newmat)) < 0 )
  137.       return code;
  138.     /* Check for the font already being in the scaled font cache. */
  139.     /* Only attempt to share fonts if the current font has */
  140.     /* a real UniqueID (i.e., not -1). */
  141. #ifdef DEBUG
  142. if ( gs_debug['m'] )
  143.    {    dprintf2("[m]UniqueID=%ld, FontType=%d,\n",
  144.       pfont->data.base.UniqueID, pfont->FontType);
  145.     dprintf6("[m]  new FontMatrix=[%g %g %g %g %g %g]\n",
  146.       pmat->xx, pmat->xy, pmat->yx, pmat->yy,
  147.       pmat->tx, pmat->ty);
  148.    }
  149. #endif
  150.     if ( pfont->data.base.UniqueID != -1 )
  151.       for ( ; pf_out != 0; prev = pf_out, pf_out = pf_out->next )
  152.         if (    pf_out->data.base.UniqueID == pfont->data.base.UniqueID &&
  153.             pf_out->FontType == pfont->FontType &&
  154.             pf_out->FontMatrix.xx == newmat.xx &&
  155.             pf_out->FontMatrix.xy == newmat.xy &&
  156.             pf_out->FontMatrix.yx == newmat.yx &&
  157.             pf_out->FontMatrix.yy == newmat.yy &&
  158.             pf_out->FontMatrix.tx == newmat.tx &&
  159.             pf_out->FontMatrix.ty == newmat.ty
  160.            )
  161.            {    *ppfont = pf_out;
  162. #ifdef DEBUG
  163. if ( gs_debug['m'] )
  164.             dprintf1("[m]found font=%lx\n", (ulong)pf_out);
  165. #endif
  166.             return 0;
  167.            }
  168.     pf_out = (gs_font *)(*pdir->alloc)(1, sizeof(gs_font), "gs_makefont");
  169.     if ( !pf_out ) return_error(gs_error_VMerror);
  170.     *pf_out = *pfont;
  171.     pf_out->FontMatrix = newmat;
  172.     if ( pdir->ssize == pdir->smax )
  173.       { /* Must discard a cached scaled font. */
  174.         /* Scan for the oldest font if we didn't already. */
  175.         if ( !prev )
  176.           for ( prev = pdir->scaled_fonts;
  177.             prev->next != 0;
  178.             prev = prev->next
  179.           ) ;
  180. #ifdef DEBUG
  181. if ( gs_debug['m'] )
  182.         dprintf1("[m]discarding font %lx\n", (ulong)prev);
  183. #endif
  184.         *pdfont = prev;
  185.         prev->prev->next = 0;
  186.       }
  187.     else
  188.       pdir->ssize++;
  189.     link_first(pdir->scaled_fonts, pf_out);
  190.     pf_out->base = pfont->base;
  191.     pf_out->dir = pdir;
  192.     *ppfont = pf_out;
  193. #ifdef DEBUG
  194. if ( gs_debug['m'] )
  195.     dprintf1("[m]new font=%lx\n", (ulong)pf_out);
  196. #endif
  197.     return 1;
  198. }
  199.  
  200. /* setfont */
  201. int
  202. gs_setfont(gs_state *pgs, gs_font *pfont)
  203. {    pgs->font = pfont;
  204.     pgs->char_tm_valid = 0;
  205.     return 0;
  206. }
  207.  
  208. /* currentfont */
  209. gs_font *
  210. gs_currentfont(gs_state *pgs)
  211. {    return pgs->font;
  212. }
  213.  
  214. /* cachestatus */
  215. void
  216. gs_cachestatus(register gs_font_dir *pdir, register uint pstat[7])
  217. {    pstat[0] = pdir->bsize;
  218.     pstat[1] = pdir->bmax;
  219.     pstat[2] = pdir->msize;
  220.     pstat[3] = pdir->mmax;
  221.     pstat[4] = pdir->csize;
  222.     pstat[5] = pdir->cmax;
  223.     pstat[6] = pdir->upper;
  224. }
  225.  
  226. /* setcachelimit */
  227. int
  228. gs_setcachelimit(gs_font_dir *pdir, uint size)
  229. {    pdir->upper = size;
  230.     return 0;
  231. }
  232.  
  233. /* setcacheparams */
  234. int
  235. gs_setcachelower(gs_font_dir *pdir, uint size)
  236. {    pdir->lower = size;
  237.     return 0;
  238. }
  239. int
  240. gs_setcacheupper(gs_font_dir *pdir, uint size)
  241. {    pdir->upper = size;
  242.     return 0;
  243. }
  244.  
  245. /* currentcacheparams */
  246. uint
  247. gs_currentcachelower(gs_font_dir *pdir)
  248. {    return pdir->lower;
  249. }
  250. uint
  251. gs_currentcacheupper(gs_font_dir *pdir)
  252. {    return pdir->upper;
  253. }
  254.  
  255. /* Dummy (ineffective) BuildChar procedure */
  256. int
  257. gs_no_build_char_proc(struct gs_show_enum_s *penum, gs_state *pgs,
  258.   gs_font *pfont, char_code chr, char *data)
  259. {    return 1;            /* failure, but not error */
  260. }
  261.  
  262. /* Purge a font from the font- and character-related caches. */
  263. /* This is only used by restore (and, someday, the GC). */
  264. void
  265. gs_purge_font_from_caches(gs_font_dir *pdir, gs_font *pfont)
  266. {
  267.     /* Purge the font from the scaled font cache. */
  268.     gs_font *pf = pdir->scaled_fonts;
  269.     while ( pf != 0 )
  270.       { if ( pf == pfont )
  271.           { pf = pf->next;
  272.         if ( pfont->prev ) pfont->prev->next = pf;
  273.         else pdir->scaled_fonts = pf;
  274.         if ( pf ) pf->prev = pfont->prev;
  275.         pdir->ssize--;
  276.           }
  277.         else if ( pf->base == pfont )
  278.           { gs_purge_font_from_caches(pdir, pf->base);
  279.         pf = pdir->scaled_fonts; /* start over */
  280.           }
  281.         else
  282.           pf = pf->next;
  283.        }
  284.  
  285.     /* Purge the font from the font/matrix pair cache, */
  286.     /* including all cached characters rendered with that font. */
  287.     gs_purge_font_from_char_caches(pdir, pfont);
  288.  
  289. }
  290.